from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import os
from models import Milestone
from forms import MilestoneForm

class ListHandler(webapp.RequestHandler):

    def get(self):
        context = {
            'title': 'Milestones',
            'instances': Milestone.all(),
            'root': 'milestones',
        }
        path = os.path.join(os.path.dirname(__file__), 
            '../templates/list.html')
        self.response.out.write(template.render(path, context))

class ItemHandler(webapp.RequestHandler):

    def get(self, key):
        milestone = Milestone.get(key)
        context = {
            'title': 'Milestone - %s' % (milestone.name, ),
            'instance': milestone,
            'root': 'milestones',
        }
        path = os.path.join(os.path.dirname(__file__), 
            '../templates/instance.html')
        self.response.out.write(template.render(path, context))

    def post(self, key):
        milestone = Milestone.get(key)
        data = MilestoneForm(data=self.request.POST, instance=milestone)
        if data.is_valid():
            # Save the data, and redirect to the view page
            entity = data.save(commit=False)
            entity.put()
            self.redirect('/projects/%s/' % (entity.project.key(), ))
        else:
            context = {
                'title': 'Create a new Milestone',
                'form': data,
                'action': '/milestones/edit/%s/' % (key, ),
            }
            path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
            self.response.out.write(template.render(path, context))

class CreateHandler(webapp.RequestHandler):

    def get(self):
        context = {
            'title': 'Create a new Milestone',
            'form': MilestoneForm(data=self.request.GET),
            'action': '/milestones/create/',
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
        self.response.out.write(template.render(path, context))

    def post(self):
        data = MilestoneForm(data=self.request.POST)
        if data.is_valid():
            # Save the data, and redirect to the view page
            entity = data.save(commit=False)
            entity.put()
            self.redirect('/projects/%s/' % (entity.project.key(), ))
        else:
            context = {
                'title': 'Create a new Milestone',
                'form': data,
                'action': '/milestones/create/',
            }
            path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
            self.response.out.write(template.render(path, context))

class EditHandler(webapp.RequestHandler):

    def get(self, key):
        milestone = Milestone.get(key)
        form = MilestoneForm(instance=milestone)
        context = {
            'title': 'Edit Milestone %s' % (milestone.name, ),
            'form': form,
            'action': '/milestones/%s/' % (milestone.key(), ),
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
        self.response.out.write(template.render(path, context))

class DeleteHandler(webapp.RequestHandler):

    def post(self, key):
        milestone = Milestone.get(key)
        project = milestone.project
        milestone.delete()
        self.redirect('/projects/%s/' % (project.key(), ))
        
def main():
    application = webapp.WSGIApplication([
        ('/milestones/', ListHandler),
        ('/milestones/create/', CreateHandler),
        (r'/milestones/edit/(.+)/', EditHandler),
        (r'/milestones/delete/(.+)/', DeleteHandler),
        (r'/milestones/(.+)/', ItemHandler),
        ], debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
